#include "ili9341_lcd.h"
#include "ili9341_spi.h"

#include "time.h"//for get fps
#include "images/images.h"


#define ILI9341_TASK_DURATION_MS            500
#define ILI9341_TASK_PRIO                   24
#define ILI9341_TASK_STACK_SIZE             1000//0x8000 //4096*2 
//bug: too large size will lead to create failed





void Test_Color(void)
{

	uint16_t figure_size = 40;
	uint16_t square_x = 0;
	uint16_t square_y = 0;
	int8_t dx = 2; // 每次移动的增量
	int8_t dy = 2;

	// int framenum=0;;

	uint16_t background = 0xF800;//0xABCD;

	// time_t  start ;
	// time_t  now ;

	// time(&start);//返回值为time_t 类型

	while(1){
		background++;

		
		// // 更新显示
		LCD_Fill(background);
 		Image image={figure_size,figure_size,gImage_qq_logo};
		LCD_DisplayImage(square_x, square_y, image);
		LCD_DisplayImage(60, 60, image);
		LCD_DisplayImage(120, 240, image);
		LCD_DisplayImage(240, 220, image);
		LCD_DisplayImage(300, 200, image);

		image.width=271;
		image.height=240;
		image.ptrArr=gImage_img2;
		LCD_DisplayImage(0, 0, image);

		osal_msleep(1000);
		// 更新正方形的位置
		square_x += dx;
		square_y += dy;

		// 边界反弹逻辑
		if (square_x <= 0 || square_x >= LCD_H - figure_size) {
			dx = -dx;
		}
		if (square_y <= 0 || square_y >= LCD_W - figure_size) {
			dy = -dy;
		}

		// 控制帧率
		//osal_msleep(16); // 大约60 FPS

		// framenum++;
		// time(&now);
		//9float fps = (float)framenum/(float)now;
		osal_printk("X: %d, Y: %d BG: %x\r\n", square_x, square_y, background);
		

	}


}


static void *ili9341_task(const char *arg)
{
	osal_printk("*ili9341_task\r\n");
    unused(arg);
    LCD_Init();
    

    Test_Color();


    return NULL;
}

static void ili9341_entry(void)
{
    osal_task *task_handle = NULL;
    osal_kthread_lock();
    task_handle = osal_kthread_create((osal_kthread_handler)ili9341_task, 0, "ili9341_task", ILI9341_TASK_STACK_SIZE);
    if (task_handle != NULL) {
        osal_kthread_set_priority(task_handle, ILI9341_TASK_PRIO);
        osal_kfree(task_handle);
    }else{
		osal_printk("ili9341_task create faile\r\n");
	}
    osal_kthread_unlock();
}

/* Run the spi_master_entry. */
app_run(ili9341_entry);